package com.blog.boot.webapp.controller;

import com.blog.boot.api.common.RestMsg;
import com.blog.boot.api.entity.UserEntity;
import com.blog.boot.api.system.service.UserService;
import com.blog.boot.webapp.controller.common.BaseController;
import com.blog.boot.webapp.utils.CommonUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@Controller
public class LoginController extends BaseController {

    @Autowired
    UserService userService;

    @RequestMapping("/user/login")
    @ResponseBody
    public RestMsg login(String name,String pass,String vercode,String referer,HttpServletRequest request){
        RestMsg msg = restFactory.getMsg();
        if(!vercode.equalsIgnoreCase((String) request.getSession().getAttribute("login_vercode"))){
            return msg.error("验证码有误");
        }
        Subject subject = SecurityUtils.getSubject();
        //数据库的密码我进行了Md5加密。如果没有进行加密的无需这个
        //user.setUserPassword(MD5Util.getPwd(user.getUserPassword()));
        UsernamePasswordToken token = new UsernamePasswordToken(name,pass);
        SecurityUtils.getSubject().logout();
        subject.login(token);
        msg.success("登录成功").put("action",referer);//获取上一个请求地址
        return msg;
    }
    @RequestMapping("/user/regist")
    @ResponseBody
    public RestMsg regist(UserEntity userEntity, String vercode,String repassword, HttpServletRequest request){
        RestMsg msg = restFactory.getMsg();
        if(!vercode.equalsIgnoreCase((String) request.getSession().getAttribute("regist_vercode"))){
            return msg.error("验证码有误");
        }
        if(!repassword.equals(userEntity.getPassword())){
            return msg.error("两次密码不一致");
        }
        if(StringUtils.isEmpty(userEntity.getEmail())){
            return msg.error("邮箱不能为空");
        }
        if(StringUtils.isEmpty(userEntity.getUsername())){
            return msg.error("昵称不能为空");
        }
        UserEntity userByUserName = userService.findUserByUserName(userEntity.getUsername());
        if( userByUserName != null ){
            return msg.error("该昵称已存在");
        }
        UserEntity userByUserEmail = userService.findUserByUserEmail(userEntity.getEmail());
        if( userByUserEmail != null ){
            return msg.error("该邮箱已存在");
        }
        UserEntity userEntity1 = new UserEntity(CommonUtils.getIp(request), CommonUtils.getSystemTime("yyyy-MM-dd"));
        UserEntity user = (UserEntity)CommonUtils.combineSydwCore(userEntity, userEntity1);
        userService.reg(user);
        msg.success("注册成功").put("action","/user/set.html");
        request.getSession().setAttribute("user",user);
        return msg;
    }

    @RequestMapping("/user/loginOut")
    public RestMsg.RestModelAndView loginOut(HttpServletRequest  request, HttpServletResponse response){
        RestMsg msg = restFactory.getMsg();
        String referer = request.getHeader("Referer");
        /*String port = String.valueOf(request.getLocalPort());
        int i = referer.indexOf(port.charAt(port.length()-1)+1);
        referer = referer.substring(i);
        System.out.println(referer);
        System.out.println(i);*/
        try {
            SecurityUtils.getSubject().logout();
            response.sendRedirect(referer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return restFactory.setModelAndView("/index.html" );
    }
    @RequestMapping("/user/loginout")
    @ResponseBody
    public RestMsg loginout(String access_token){
        RestMsg msg = restFactory.getMsg();
        SecurityUtils.getSubject().logout();
        return msg;
    }
}
